package com.easy.web.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtil {

	private static final String DATETIME_PATTERN = "yyyyMMddHHmmss";
	private static final String DATE_PATTERN = "yyyyMMdd";
	private static final String DATETIME_MILLISECOND_PATTERN =  "yyyyMMddHHmmssSSS";
	private static final String DATETIME_PATTERN_ = "yyyy/MM/dd HH:mm:ss";
	private static final String DATETIME_PAT = "yyyy/MM/dd";
	
	
	/**
	 * 获取系统当前时间带毫秒时间
	 * 
	 * @return
	 */
	public static String getCurrentTimeMillisecond() {
		SimpleDateFormat sdf = new SimpleDateFormat(DATETIME_MILLISECOND_PATTERN);
		return sdf.format(new Date());
	}

	/**
	 * 获取系统当前时间
	 * 
	 * @return
	 */
	public static String getCurrentTime() {
		SimpleDateFormat sdf = new SimpleDateFormat(DATETIME_PATTERN);
		return sdf.format(new Date());
	}

	/**
	 * 获取系统当前日期
	 * 
	 * @return
	 */
	public static String getCurrentDate() {
		SimpleDateFormat sdf = new SimpleDateFormat(DATE_PATTERN);
		return sdf.format(new Date());
	}

	public static String formatDate(Date date) {
		SimpleDateFormat sdf = new SimpleDateFormat(DATE_PATTERN);
		return sdf.format(date);
	}

	public static String formatDateTime(Date date) {
		SimpleDateFormat sdf = new SimpleDateFormat(DATETIME_PATTERN);
		return sdf.format(date);
	}

	public static Date parseDate(String dateString) {
		try {
			SimpleDateFormat sdf = new SimpleDateFormat(DATETIME_PATTERN);
			return sdf.parse(dateString);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return null;
	}

	public static Date parseDateTime(String dateString) {
		try {
			SimpleDateFormat sdf = new SimpleDateFormat(DATETIME_PATTERN);
			return sdf.parse(dateString);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return null;
	}
	public static Date parseDatePaf(String dateString) {
		try {
			SimpleDateFormat sdf = new SimpleDateFormat(DATETIME_PATTERN_);
			return sdf.parse(dateString);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return null;
	}

	public static Date parseDatePf(String dateString) {
		try {
			SimpleDateFormat sdf = new SimpleDateFormat(DATETIME_PAT);
			return sdf.parse(dateString);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 
	 * @param standardDataTimeString
	 *            yyyy-MM-dd HH:mm:ss 或者 yyyy-MM-dd
	 * @return yyyyMMddHHmmss 或者 yyyyMMdd
	 */
	public static String parseDateToSimplified(String standardDataTimeString) {
		return standardDataTimeString.replaceAll("[\\-\\s\\:]", "");
	}

	/**
	 * 
	 * @param simplifiedDataTimeString
	 *            yyyyMMddHHmmss 或者 yyyyMMdd
	 * @return yyyy-MM-dd HH:mm:ss 或者 yyyy-MM-dd
	 */
	public static String parseDataToStandard(String simplifiedDataTimeString) {
		StringBuffer dateBuffer = new StringBuffer();
		dateBuffer.append(simplifiedDataTimeString.substring(0, 4)).append("-")
				  .append(simplifiedDataTimeString.subSequence(4, 6)).append("-")
				  .append(simplifiedDataTimeString.subSequence(6, 8));
		if (simplifiedDataTimeString.length() == 14) {
			dateBuffer.append(" ")
					  .append(simplifiedDataTimeString.substring(8, 10)).append(":")
					  .append(simplifiedDataTimeString.substring(10, 12)).append(":")
					  .append(simplifiedDataTimeString.subSequence(12, 14));
		}
		return dateBuffer.toString();
	}

	public static int compareDate(String sd1, String sd2) {
		Date d1 = parseDate(sd1);
		Date d2 = parseDate(sd2);
		return d1.compareTo(d2);
	}

	public static void add(Date date, int field, int amount) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(field, amount);
		date.setTime(cal.getTimeInMillis());
	}

	/**
	 * 将时间转化成yyyy-MM-dd HH:mm:ss 时间格式字符串
	 * 
	 * @param time
	 * @return
	 */
	public static String longToString(String time) {
		Date date = new Date(Long.parseLong(time.trim()) * 1000);
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String dateString = formatter.format(date);
		return dateString;
	}

	/**
	 * 将yyyy-MM-dd HH:mm:ss 时间格式字符串转化成时间
	 * 
	 * @param time
	 * @return
	 */
	public static String StringToDate(String time) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

		long millionSeconds = 0;
		try {
			millionSeconds = sdf.parse(time).getTime() / 1000;
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return Long.toString(millionSeconds);
	}

	/**
	 * 判断目标日期是否在2个时间段内
	 *
	 */
	public static boolean between(Date dest,Date start,Date end){

		if(dest.getTime()<start.getTime()||dest.getTime()>end.getTime()){
			return true;
		}
		return false;
	}

	/**
	 * 计算2个时间段的差
	 */

	public static String diff(Date oldDate,Date nowDate){
		String result="";
		long diff=nowDate.getTime()-oldDate.getTime();

		long diffSeconds = diff / 1000 % 60;
		long diffMinutes = diff / (60 * 1000) % 60;
		long diffHours = diff / (60 * 60 * 1000) % 24;
		long diffDays = diff / (24 * 60 * 60 * 1000);

		if(diffDays>0){
			result+=diffDays+"天";
		}
		if(diffHours>0){
			result+=diffHours+"小时";
		}
		if(diffMinutes>0){
			result+=diffMinutes+"分钟";
		}
		if(diffSeconds>0){
			result+=diffMinutes+"秒";
		}
		return result;
	}

	/**
	 *  当前时间加上时间段
	 */

	public static Date getAddDate(Date date,long times){
		long newTime=date.getTime()+times;

		return new Date(newTime);
	}

	/**
	 *  当前时间减去时间段
	 */

	public static Date getDecreaseDate(Date date,long times){
		long newTime=date.getTime()-times;

		return new Date(newTime);
	}

	/**
	 * 获取前几天的日期
	 */
	public static Date getDateBefore(int day){
		Calendar c = Calendar.getInstance();
		Date newDate=new Date();
		c.setTime(newDate);
		c.set(Calendar.DATE, c.get(Calendar.DATE) - day);
		return c.getTime();
	}

}
